#!/bin/bash
#############################################################################
#  Copyright (C) 2013-2015 Lawrence Livermore National Security, LLC.
#  Produced at Lawrence Livermore National Laboratory (cf, DISCLAIMER).
#  Written by Albert Chu <chu11@llnl.gov>
#  LLNL-CODE-644248
#
#  This file is part of Magpie, scripts for running Hadoop on
#  traditional HPC systems.  For details, see https://github.com/llnl/magpie.
#
#  Magpie is free software; you can redistribute it and/or modify it
#  under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#
#  Magpie is distributed in the hope that it will be useful, but
#  WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
#  General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with Magpie.  If not, see <http://www.gnu.org/licenses/>.
#############################################################################

# These are functions to be called by magpie-run

source ${MAGPIE_SCRIPTS_HOME}/magpie/exports/magpie-exports-submission-type
source ${MAGPIE_SCRIPTS_HOME}/magpie/exports/magpie-exports-dirs
source ${MAGPIE_SCRIPTS_HOME}/magpie/exports/magpie-exports-user
source ${MAGPIE_SCRIPTS_HOME}/magpie/lib/magpie-lib-defaults
source ${MAGPIE_SCRIPTS_HOME}/magpie/lib/magpie-lib-node-identification
source ${MAGPIE_SCRIPTS_HOME}/magpie/lib/magpie-lib-run
source ${MAGPIE_SCRIPTS_HOME}/magpie/lib/magpie-lib-log
source ${MAGPIE_SCRIPTS_HOME}/magpie/lib/magpie-lib-paths

# Return 0 if service up, 1 if not
__Magpie_run_check_storm_up () {
    cd ${STORM_HOME}

    # example output : "No topologies running"
    if ${stormcmdprefix}/storm list | grep -q "No topologies running"
    then
        return 0
    fi

    return 1
}

Magpie_run_start_storm () {
    if [ "${STORM_SETUP}" == "yes" ] && [ "${magpie_run_prior_startup_successful}" == "true" ]
    then
        if [ "${magpie_run_zookeeper_setup_successful}" == "0" ]
        then
            Magpie_output_internal_error "Attempt to setup Storm without Zookeeper being setup"
            magpie_run_storm_should_be_torndown=0
            magpie_run_storm_setup_successful=0
            return 1
        fi

        if [ ${MAGPIE_JOB_TYPE} != "setuponly" ]
        then
            # Make variables unspecified for launching
            Magpie_make_all_local_dirs_unspecified

            echo "Starting storm"
            ${MAGPIE_SCRIPTS_HOME}/bin/magpie-storm-start-all.sh

            # Make variables specific now within Magpie
            Magpie_make_all_local_dirs_node_specific

            # My rough estimate for setup time is 30 seconds per 128 nodes
            local sleepwait=`expr ${STORM_WORKER_COUNT} \/ 128 \* 30`
            if [ ${sleepwait} -lt 30 ]
            then
                sleepwait=30
            fi
            echo "Waiting ${sleepwait} seconds to allow Storm daemons to setup"
            sleep ${sleepwait}
            magpie_run_total_sleep_wait=`expr ${magpie_run_total_sleep_wait} + ${sleepwait}`
        fi

        echo "*******************************************************"
        echo "*"
        echo "* Storm Information"
        echo "*"
        echo "* You can view your Storm status by launching a web browser and pointing to ..."
        echo "*"
        echo "* Storm UI: http://${STORM_MASTER_NODE}:${default_storm_ui_port}"
        echo "*"
        echo "* To access Storm directly, you'll want to:"
        echo "*"
        echo "*   ${MAGPIE_REMOTE_CMD:-ssh}${MAGPIE_REMOTE_CMD_OPTS:+" "}${MAGPIE_REMOTE_CMD_OPTS} ${STORM_MASTER_NODE}"
        if echo $MAGPIE_SHELL | grep -q csh
        then
            echo "*   setenv JAVA_HOME \"${JAVA_HOME}\""
            echo "*   setenv STORM_HOME \"${STORM_HOME}\""
            echo "*   setenv STORM_CONF_DIR \"${STORM_CONF_DIR}\""
            if [ "${MAGPIE_NO_LOCAL_DIR}" == "yes" ]
            then
                echo "*   setenv STORM_JAR_JVM_OPTS \"-Djava.io.tmpdir=${STORM_LOCAL_SCRATCHSPACE_DIR} -XX:-UsePerfData\""
            fi
        else
            echo "*   export JAVA_HOME=\"${JAVA_HOME}\""
            echo "*   export STORM_HOME=\"${STORM_HOME}\""
            echo "*   export STORM_CONF_DIR=\"${STORM_CONF_DIR}\""
            if [ "${MAGPIE_NO_LOCAL_DIR}" == "yes" ]
            then
                echo "*   export STORM_JAR_JVM_OPTS=\"-Djava.io.tmpdir=${STORM_LOCAL_SCRATCHSPACE_DIR} -XX:-UsePerfData\""
            fi
        fi
        echo "*"
        echo "* Then you can do as you please.  For example to run a job:"
        echo "*"
        echo "*   \$STORM_HOME/${stormcmdprefix}/storm jar ..."
        echo "*"
        if [ "${MAGPIE_JOB_TYPE}" == "setuponly" ]
        then
            echo "* To setup, login and set environment variables per the"
            echo "* instructions above, then run:"
            echo "*"
            echo "*   $MAGPIE_SCRIPTS_HOME/bin/magpie-storm-start-all.sh"
            echo "*"
            echo "* To end/cleanup your session & kill all daemons, login and set"
            echo "* environment variables per the instructions above, then run:"
            echo "*"
            echo "*   $MAGPIE_SCRIPTS_HOME/bin/magpie-storm-stop-all.sh"
            echo "*"
        fi
        if [ "${MAGPIE_ENVIRONMENT_VARIABLE_SCRIPT}X" != "X" ]
        then
            echo "* If running interactively, sourcing"
            echo "*"
            echo "* ${MAGPIE_ENVIRONMENT_VARIABLE_SCRIPT}"
            echo "*"
            echo "* will set most common environment variables for your job."
            echo "*"
        fi
        echo "*******************************************************"

        if [ "${MAGPIE_ENVIRONMENT_VARIABLE_SCRIPT}X" != "X" ]
        then
            if echo $MAGPIE_SHELL | grep -q csh
            then
                echo "setenv STORM_HOME \"${STORM_HOME}\"" >> ${MAGPIE_ENVIRONMENT_VARIABLE_SCRIPT}
                echo "setenv STORM_CONF_DIR \"${STORM_CONF_DIR}\"" >> ${MAGPIE_ENVIRONMENT_VARIABLE_SCRIPT}
                echo "setenv STORM_LOCAL_SCRATCHSPACE_DIR \"${STORM_LOCAL_SCRATCHSPACE_DIR}\"" >> ${MAGPIE_ENVIRONMENT_VARIABLE_SCRIPT}
                echo "setenv STORM_LOG_DIR \"${STORM_LOG_DIR}\"" >> ${MAGPIE_ENVIRONMENT_VARIABLE_SCRIPT}
                echo "setenv STORM_MASTER_NODE \"${STORM_MASTER_NODE}\"" >> ${MAGPIE_ENVIRONMENT_VARIABLE_SCRIPT}
                echo "setenv STORM_NIMBUS_HOST \"${STORM_NIMBUS_HOST}\"" >> ${MAGPIE_ENVIRONMENT_VARIABLE_SCRIPT}
                echo "setenv STORM_WORKER_COUNT \"${STORM_WORKER_COUNT}\"" >> ${MAGPIE_ENVIRONMENT_VARIABLE_SCRIPT}
                echo "setenv STORM_VERSION \"${STORM_VERSION}\"" >> ${MAGPIE_ENVIRONMENT_VARIABLE_SCRIPT}
                if [ "${MAGPIE_NO_LOCAL_DIR}" == "yes" ]
                then
                    echo "setenv STORM_JAR_JVM_OPTS \"-Djava.io.tmpdir=${STORM_LOCAL_SCRATCHSPACE_DIR} -XX:-UsePerfData\"" >> ${MAGPIE_ENVIRONMENT_VARIABLE_SCRIPT}
                fi
            else
                echo "export STORM_HOME=\"${STORM_HOME}\"" >> ${MAGPIE_ENVIRONMENT_VARIABLE_SCRIPT}
                echo "export STORM_CONF_DIR=\"${STORM_CONF_DIR}\"" >> ${MAGPIE_ENVIRONMENT_VARIABLE_SCRIPT}
                echo "export STORM_LOCAL_SCRATCHSPACE_DIR=\"${STORM_LOCAL_SCRATCHSPACE_DIR}\"" >> ${MAGPIE_ENVIRONMENT_VARIABLE_SCRIPT}
                echo "export STORM_LOG_DIR=\"${STORM_LOG_DIR}\"" >> ${MAGPIE_ENVIRONMENT_VARIABLE_SCRIPT}
                echo "export STORM_MASTER_NODE=\"${STORM_MASTER_NODE}\"" >> ${MAGPIE_ENVIRONMENT_VARIABLE_SCRIPT}
                echo "export STORM_NIMBUS_HOST=\"${STORM_NIMBUS_HOST}\"" >> ${MAGPIE_ENVIRONMENT_VARIABLE_SCRIPT}
                echo "export STORM_WORKER_COUNT=\"${STORM_WORKER_COUNT}\"" >> ${MAGPIE_ENVIRONMENT_VARIABLE_SCRIPT}
                echo "export STORM_VERSION=\"${STORM_VERSION}\"" >> ${MAGPIE_ENVIRONMENT_VARIABLE_SCRIPT}
                if [ "${MAGPIE_NO_LOCAL_DIR}" == "yes" ]
                then
                    echo "export STORM_JAR_JVM_OPTS=\"-Djava.io.tmpdir=${STORM_LOCAL_SCRATCHSPACE_DIR} -XX:-UsePerfData\"" >> ${MAGPIE_ENVIRONMENT_VARIABLE_SCRIPT}
                fi
            fi
            echo "" >> ${MAGPIE_ENVIRONMENT_VARIABLE_SCRIPT}
        fi

        if [ ${MAGPIE_JOB_TYPE} != "setuponly" ]
        then
            # Return 0 if service up, 1 if not
            Magpie_check_service_up "Storm" "__Magpie_run_check_storm_up"

            if [ $? -eq 0 ]
            then
                magpie_run_storm_should_be_torndown=1
                magpie_run_storm_setup_successful=1
            else
                magpie_run_storm_should_be_torndown=1
                magpie_run_storm_setup_successful=0
                magpie_run_prior_startup_successful=false
            fi
        else
            magpie_run_storm_should_be_torndown=1
            magpie_run_storm_setup_successful=1
        fi
    else
        magpie_run_storm_should_be_torndown=0
        magpie_run_storm_setup_successful=1
    fi
}

Magpie_run_storm () {
    if [ "${STORM_JOB}" == "stormwordcount" ]
    then
        echo "*******************************************************"
        echo "* Running Storm WordCount"
        echo "*******************************************************"
        ${MAGPIE_SCRIPTS_HOME}/magpie/run/magpie-run-execute script ${MAGPIE_SCRIPTS_HOME}/magpie/job/magpie-job-storm-stormwordcount &
        local scriptpid=$!
        Magpie_wait_script_sigusr2_on_job_timeout ${scriptpid}
    else
        Magpie_output_internal_error "STORM_JOB = ${STORM_JOB} not handled"
    fi
}

Magpie_run_stop_storm () {
    if [ "${magpie_run_zookeeper_teardown_complete}" == "1" ]
    then
        Magpie_output_internal_warning "tearing down Storm after Zookeeper"
    fi

    if [ "${STORM_SETUP}" == "yes" ] && [ "${magpie_run_storm_should_be_torndown}" == "1" ]
    then
        if [ ${MAGPIE_JOB_TYPE} != "setuponly" ]
        then
            # Make variables unspecified for shutdown
            Magpie_make_all_local_dirs_unspecified

            echo "Stopping storm"
            ${MAGPIE_SCRIPTS_HOME}/bin/magpie-storm-stop-all.sh

            # Make variables specific now within Magpie
            Magpie_make_all_local_dirs_node_specific
        fi
    fi
    magpie_run_storm_teardown_complete=1
}
